//
//  RTJDebtCalendarController.m
//  RuiTaiJinProject
//
//  Created by jayson on 2019/3/27.
//  Copyright © 2019年 leewei. All rights reserved.
//

#import "RTJDebtCalendarController.h"
#import "RTJDebtCalendarCell.h"
#import "RTJDebtCalendarTwoCell.h"

@interface RTJDebtCalendarController ()<SelectedDayDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *data;
@property (nonatomic, strong) NSDictionary *dict;
@property (nonatomic, strong) NSMutableDictionary *already_repay;
@property (nonatomic, strong) NSMutableDictionary *unRepay;
@property (nonatomic,strong) NSString *selectedDay;
@property (assign, nonatomic) BOOL isUpdate;


@end



@implementation RTJDebtCalendarController

-(NSMutableArray *)data {
    if (!_data) {
        _data = [NSMutableArray array];
    }
    
    return _data;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"selectedDay"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    self.title = @"回款日历";
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    // 设置日期格式
    [formatter setDateFormat:@"yyyy-MM-dd"];
    // 获取当前日期
    NSDate *currentDate = [NSDate date];
    NSString *currentDateString = [formatter stringFromDate:currentDate];
    _selectedDay = currentDateString;
    
    self.tableView.tableFooterView=[UIView new];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    self.tableView.estimatedRowHeight=300;
    
    [self.tableView reloadData];

    self.tableView.mj_header = [CustomRefreshGifHeader headerWithRefreshingBlock:^{

        [self loadData];

    }];

    [self.tableView.mj_header beginRefreshing];
    
}

-(void)loadData{
    
    [SVProgressHUD  showWithStatus:nil];
    
    if (self.data.count == 0) {
        
        [self.view showEmptyViewWithStr:@"" image:nil callback:nil];
        
    }
    
    [RTJNetWork asyncRequestWithURL:@"user/repay_calendar" params:nil method:GET zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
        
        [SVProgressHUD dismiss];
        [self.view removeEmptyView];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        
        if (success) {
            
            if ([data[@"status"]integerValue] == 1) {
                
                id dic = data[@"data"];
                if ([dic isKindOfClass:[NSDictionary class]]&& dic!=nil) {
                    
                    [self.data removeAllObjects];

                    self.dict = dic;
                    [self updata];

                }else{
                    
                    [self.view showEmptyViewWithStr:@"暂无相关数据" image:[UIImage imageNamed:@"kbylogo"] callback:^{
                        [self loadData];
                    }];
                    
                }
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",data[@"msg"]]];
                
                [self.view showEmptyViewWithStr:errorMsg image:[UIImage imageNamed:@"kbylogo"] callback:^{
                    [self loadData];
                }];
                
            }
            
        }else{
            
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",errorMsg]];
            
            [self.view showEmptyViewWithStr:errorMsg image:[UIImage imageNamed:@"kbylogo"] callback:^{
                [self loadData];
            }];
            
        }
        
    }];
}

-(void)selectedCurrentDay:(NSString *)dayStr isUpdate:(BOOL)isUpdate{
    
   [self.data removeAllObjects];
    self.selectedDay = dayStr;
    self.isUpdate = isUpdate;
    [self updata];
    
}

-(void)updata{
    
    id arr = _dict[@"deal_repay"];
        if ([arr isKindOfClass:[NSArray class]]&& [arr count]!=0) {
            
            [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

                if ( [obj[@"time"] isEqualToString:self.selectedDay]) {

                    [self.data addObject:obj];
              
                }
                
                if (self.isUpdate) {
                    
                    if (idx == [arr count]-1){
                        
                        NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:1];
                        [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
                    }
                    
                }else{
                    
                        [self.tableView reloadData];

                }
            }];
            
        }else{
            
            if (self.isUpdate) {
                
                NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:1];
                [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
                
            }else{
                
                [self.tableView reloadData];

            }

        }
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    

    if (section == 1) {
        
        return  self.data.count+1;
        
    }else{
        
        return 1;
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
  
    return UITableViewAutomaticDimension;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        static NSString * ID = @"RTJDebtCalendarCell";
        
        RTJDebtCalendarCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        
            if (!cell) {
                
                cell = [[NSBundle mainBundle] loadNibNamed:@"RTJDebtCalendarCell" owner:self options:nil][0];
            }
        cell.data = _dict;
        cell.delegate = self;
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        
        return cell;

    }else{
        
        static NSString * ID = @"RTJDebtCalendarTwoCell";
        
        RTJDebtCalendarTwoCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        
        if (indexPath.row == 0) {
            
            if (!cell) {
                
                cell = [[NSBundle mainBundle] loadNibNamed:@"RTJDebtCalendarTwoCell" owner:self options:nil][1];
            }
            cell.isShow = _data.count!=0 ? NO : YES;
            cell.data = _dict;
            
        }else{
            
            if (!cell) {
                
                cell = [[NSBundle mainBundle] loadNibNamed:@"RTJDebtCalendarTwoCell" owner:self options:nil][0];
            }
            
            cell.dic =_data.count!=0 ? _data[indexPath.row-1] :nil;

        }
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
          
        return cell;
        
    }
   
    return  nil;
    
}

@end
